<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/6/23
 * Time: 9:12 AM
 */
namespace app\common\routes;

use app\common\routes\ICmsRoute;
use Illuminate\Support\Str;
use app\common\exceptions\BizException;

abstract class AbstractCmsRoute implements ICmsRoute
{
    public $path = '';

    protected $middleware = [];

    public function __construct($path)
    {
        $this->path = $path;
    }

    public function pluginMatch($routes)
    {
        $class_name = '';
        $action = '';
        $plugin = array_shift($routes);
        //todo
        $plugin_app = app('plugins')->getEnablePlugin($plugin);
        if (empty($plugin_app)) {
            throw new BizException('插件不存在或未安装');
        }
        $namespace = $plugin_app->namespace;
        foreach ($routes as $route) {
            if ($class_name) {
                $action = $route;
                break;
            }
            $controller = ucfirst(Str::camel($route)).'Controller';
            if (class_exists($namespace.'\\'.$controller)) {
                $class_name = $namespace.'\\'.$controller;
            } else {
                $namespace .= '\\'.$route;
            }
        }
        return [$class_name,$action];
    }


    abstract public function routeMatch($routes,$first);

    public function getMiddleware()
    {
        return $this->middleware;
    }
}